package com.du.design.status;

import cn.hutool.json.JSONUtil;

import java.util.List;
import java.util.Optional;

/**
 * @desc:
 * @author: shuangdu
 * @date: 2021/9/7
 * @since: 1.8
 */
public class StateMachine {

    public static void execute(StateMachineTransaction stateMachineTransaction) {
        stateMachineTransaction.getEvent().process(stateMachineTransaction);

    }

    public static void main(String[] args) {
        DataNodeList nodeList = new DataNodeList();
        List<NodeChain> nodes = nodeList.getNodes();

        System.out.println(nodes);
        NodeChain current = nodeList.currentNode(0);
        System.out.println(current);

        StateMachineTransaction stateMachineTransaction =
        StateMachineTransaction.builder()
                .current(current)
                .nodes(nodes)
                .applyStatus(ApplyStatus.APPLY)
                .action(ExamState.PASS)
                .event((stateMachine) -> {
//                    System.out.println(JSONUtil.toJsonStr(stateMachine));
                    if (stateMachine.getCurrent() == null) {
                        throw new NullPointerException("current most be not null!");
                    }
                    Optional<NodeChain> first = stateMachine.getNodes().stream()
                            .filter(i -> i.getName().equals(stateMachine.getCurrent().getName()))
                            .findFirst();
                    if (first.isPresent()) {
                        NodeChain nodeChain = first.get();
                        // 前进/后退


                    }

                })
                .build();

        execute(stateMachineTransaction);

    }
}
